<?php


namespace App\Repositories\Ota\Qn\Policy\Eloquent;


use App\Repositories\Ota\Qn\Policy\Contracts\QuPolicyInterface;
use App\Repositories\Ota\Qn\Policy\Service\QnPolicyService;
use App\Repositories\Policy\Eloquent\QuRepository;
use App\Services\Xml\Xml;
use Curl\CurlService\CurlService;
use Curl\StrService\DateService;

class QuPolicyRepository extends QuRepository implements QuPolicyInterface
{

    private $compareUrl = 'https://apiqae.qunar.com/api/router';

    /**
     * 去哪的比价
     * @param $param
     * @return mixed|void
     */
    public function lowPricePolicyList($param)
    {
        //param参数
        $params['dpt']              = $param['dpt'];
        $params['arr']              = $param['arr'];
        $params['flightDate']       = $param['flightDate'];
        $params['domain']           = !empty($param['domain']) ? $param['domain'] :'agb.trade.qunar.com';

         if (!empty($param['carrier'])) {

             $params['carrier'] = $param['carrier'];
         }

        if (!empty($param['flightNo'])) {

            $params['flightNo'] = $param['flightNo'];
        }

        if (!empty($param['productMark'])) {

            $params['productMark'] = $param['productMark'];
        }


        $time = DateService::mtime();

        $data['tag']                = 'flight.national.tts.bid.price';
        $data['token']              = $this->token[$params['domain']];
        $data['createTime']         = $time;
        $data['params']             = json_encode($params,JSON_UNESCAPED_UNICODE);
        $data['key']                = $this->key[$params['domain']];
        ksort($data);
        $signStr = '';
        foreach ($data as $key=>$value) {

            $signStr.=$key.'='.$value;
        }

        $data['sign']              = md5($signStr);
        unset($data['key']);
        $res = CurlService::get($this->compareUrl.'?'.http_build_query($data));
        return $res;
    }


    /**
     * 上传政策代码
     * @param $param
     * @return mixed|void
     */
    public function add($param,$del = true)
    {

        switch ($param[0]['type']) {

            case 'SINGLE':
            case 'ROUND':
            case 'STOCK': return $this->uploadStock($param,$del);
        }
    }

    /**
     * 分销上传政策代码
     * @param $param
     * @return mixed|void
     */
    public function addFenXiao($param)
    {

        switch ($param[0]['type']) {

            case 'SINGLE':
            case 'STOCK':
                return $this->fxUploadStock($param);
        }
    }

    /**
     * 删除政策代码
     * @param $param
     */
    public function delete($param)
    {
        $xml = new Xml();

        //创建根节点
        $this->userInfo['PolicyList']['type']   = $param['type'];
        $this->userInfo['PolicyList']['ext']    = $param['ext'].':delete';
        $this->userInfo['PolicyList']['url']    = $param['url'];
        $node = $xml->createNode($this->userInfo);

        foreach ($param['flights'] as $key=>$value) {

            $deleteProduct = [
                'type'              => $value['type'],
                'policyCode'        => $value['policyCode']
            ];
            $xml->createNode(['DeletePolicy'=>$deleteProduct],$node);
        }

        return $this->uploadPolicy($this->urls[$param['type']],$xml);
    }

    /**
     * 上传统一库存政策
     * @param $param
     * @return string
     */
    private function uploadStock($param,$del = true){

        $xml = new Xml();

        $firstItem = current($param);

        $this->userInfo['PolicyList']['type']       = $firstItem['type'];
        $this->userInfo['PolicyList']['execType']   = 'ADD';
        $this->userInfo['PolicyList']['ext']        = $firstItem['ext'].':add';
        $this->userInfo['PolicyList']['url']        = $firstItem['url'];

        //创建根节点
        $node = $xml->createNode($this->userInfo);

        if ($del) {

            $deleteNode['DeletePolicy'] = [
                'type'          => $firstItem['type'],
                'policyCode'    => $firstItem['policycode']
            ];

            //创建根节点
            $node = $xml->createNode($deleteNode,$node);
        }



        if ($firstItem['type'] != 'ROUND') {
            QnPolicyService::signalStockPolicy($param,$xml,$node);
        } else {
            QnPolicyService::roundPolicy($param,$xml,$node);
        }


        return $this->uploadPolicy($this->urls[$firstItem['type']],$xml,null);
    }


    /**
     * 分销上传统一库存政策
     * @param $param
     * @return string
     */
    private function fxUploadStock($param){

        $xml = new Xml();

        $firstItem = current($param);

        $this->userInfo['PolicyList']['type']       = $firstItem['type'];
        $this->userInfo['PolicyList']['execType']   = 'ADD';
        $this->userInfo['PolicyList']['ext']        = $firstItem['ext'].':add';
        $this->userInfo['PolicyList']['url']        = $firstItem['url'];

        //创建根节点
        $node = $xml->createNode($this->userInfo);

        $deleteNode['DeletePolicy'] = [
            'type'          => $firstItem['type'],
            'policyCode'    => $firstItem['policycode']
        ];

        //创建根节点
        $node = $xml->createNode($deleteNode,$node);

        QnPolicyService::signalStockPolicy($param,$xml,$node);

        return $this->uploadPolicy($this->qnFenXiaoUrls[$firstItem['type']],$xml,null);
    }

    /**
     * 分销删除政策代码
     * @param $param
     */
    public function fxDelete($param)
    {
        $xml = new Xml();

        //创建根节点
        $this->userInfo['PolicyList']['type']   = $param['type'];
        $this->userInfo['PolicyList']['ext']    = $param['ext'].':delete';
        $this->userInfo['PolicyList']['url']    = $param['url'];
        $node = $xml->createNode($this->userInfo);

        foreach ($param['flights'] as $key=>$value) {

            $deleteProduct = [
                'type'              => $value['type'],
                'policyCode'        => $value['policyCode']
            ];
            $xml->createNode(['DeletePolicy'=>$deleteProduct],$node);
        }

        return $this->uploadPolicy($this->qnFenXiaoUrls[$param['type']],$xml);
    }
}